package com.baihan.angus.app.game.busshow.adps;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.ScriptStepResponse;

import java.util.List;

public class QuestionOptionItemAdp extends RecyclerView.Adapter {

    private Context mContext;
    private List<ScriptStepResponse.DataX.Question.Data.Options> mOptionList;
    private RecyclerView mRecyclerView;
    private int category;//0单选 1多选

    public QuestionOptionItemAdp(Context mContext, List<ScriptStepResponse.DataX.Question.Data.Options> mOptionList, int category) {
        this.mContext = mContext;
        this.mOptionList = mOptionList;
        this.category = category;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question_option, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;

        ScriptStepResponse.DataX.Question.Data.Options data = mOptionList.get(position);

        vh.tvOption.setText(data.getOp_name());
        vh.tvContent.setText(data.getOp_value());

        vh.tvOption.setTextColor(data.isSelected() ? mContext.getColor(R.color.appColor) : mContext.getColor(R.color.bg4));
        vh.tvContent.setTextColor(data.isSelected() ? mContext.getColor(R.color.appColor) : mContext.getColor(R.color.bg4));

        vh.llOptionItem.setOnClickListener(v -> {
            if (category == 1) {//多选
                data.setSelected(!data.isSelected());
            } else if (category == 0) {//单选
                for (int i = 0; i < mOptionList.size(); i++) {
                        if (mOptionList.get(i).isSelected()) {
                            mOptionList.get(i).setSelected(false);
                        }
                }

                data.setSelected(!data.isSelected());
            }
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return mOptionList.size();
    }

    /**
     * 绑定recycleview
     *
     * @param recyclerView
     */
    public void bindRecycleView(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        recyclerView.setAdapter(this);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvOption, tvContent;
        LinearLayout llOptionItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvOption = itemView.findViewById(R.id.tvOption);
            tvContent = itemView.findViewById(R.id.tvContent);
            llOptionItem = itemView.findViewById(R.id.llOptionItem);
        }
    }
}
